package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.DictData;

/**
 * 字典表 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface DictDataMapper {
	
	/**
	 * 根据条件分页查询字典数据
	 * 
	 * @param dictData 字典数据信息
	 * @return 字典数据集合信息
	 */
	@Select({"<script>",
		"select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark",
		"from t_dict_data",
		"<where>",
		"<if test=\"dictType != null and dictType != ''\">",
		"AND dict_type = #{dictType}",
		"</if>",
		"<if test=\"dictLabel != null and dictLabel != ''\">",
		"AND dict_label like concat('%', #{dictLabel}, '%')",
		"</if>",
		"<if test=\"status != null and status != ''\">",
		"AND status = #{status}",
		"</if>",
		"</where>",
		"</script>"})
	public List<DictData> selectDictDataList(DictData dictData);

	/**
	 * 根据字典类型查询字典数据
	 * 
	 * @param dictType 字典类型
	 * @return 字典数据集合信息
	 */
	@Select("select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark " +
			"from t_dict_data " +
			"where status = '0' and dict_type = #{dictType} order by dict_sort asc")
	public List<DictData> selectDictDataByType(String dictType);

	/**
	 * 根据字典类型和字典键值查询字典数据信息
	 * 
	 * @param dictType  字典类型
	 * @param dictValue 字典键值
	 * @return 字典标签
	 */
	@Select("select dict_label from t_dict_data " +
			"where dict_type = #{dictType} and dict_value = #{dictValue}")
	public String selectDictLabel(@Param("dictType") String dictType, @Param("dictValue") String dictValue);

	/**
	 * 根据字典数据ID查询信息
	 * 
	 * @param dictCode 字典数据ID
	 * @return 字典数据
	 */
	@Select("select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark " +
			"from t_dict_data " +
			"where dict_code = #{dictCode}")
	public DictData selectDictDataById(Long dictCode);

	/**
	 * 查询字典数据
	 * 
	 * @param dictType 字典类型
	 * @return 字典数据
	 */
	@Select("select count(1) from t_dict_data where dict_type=#{dictType}")
	public int countDictDataByType(String dictType);

	/**
	 * 通过字典ID删除字典数据信息
	 * 
	 * @param dictCode 字典数据ID
	 * @return 结果
	 */
	@Delete("delete from t_dict_data where dict_code = #{dictCode}")
	public int deleteDictDataById(Long dictCode);

	/**
	 * 批量删除字典数据
	 * 
	 * @param ids 需要删除的数据
	 * @return 结果
	 */
	@Delete({"<script>",
		"delete from t_dict_data where dict_code in",
		"<foreach collection=\"array\" item=\"dictCode\" open=\"(\" separator=\",\" close=\")\">",
		"#{dictCode}",
		"</foreach>",
		"</script>"})
	public int deleteDictDataByIds(String[] ids);

	/**
	 * 新增字典数据信息
	 * 
	 * @param dictData 字典数据信息
	 * @return 结果
	 */
	@Insert({"<script>",
		"insert into t_dict_data(",
		"<if test=\"dictSort != null\">dict_sort,</if>",
		"<if test=\"dictLabel != null and dictLabel != ''\">dict_label,</if>",
		"<if test=\"dictValue != null and dictValue != ''\">dict_value,</if>",
		"<if test=\"dictType != null and dictType != ''\">dict_type,</if>",
		"<if test=\"cssClass != null and cssClass != ''\">css_class,</if>",
		"<if test=\"listClass != null and listClass != ''\">list_class,</if>",
		"<if test=\"isDefault != null and isDefault != ''\">is_default,</if>",
		"<if test=\"status != null\">status,</if>",
		"<if test=\"remark != null and remark != ''\">remark,</if>",
		"<if test=\"createBy != null and createBy != ''\">create_by,</if>",
		"create_time",
		")values(",
		"<if test=\"dictSort != null\">#{dictSort},</if>",
		"<if test=\"dictLabel != null and dictLabel != ''\">#{dictLabel},</if>",
		"<if test=\"dictValue != null and dictValue != ''\">#{dictValue},</if>",
		"<if test=\"dictType != null and dictType != ''\">#{dictType},</if>",
		"<if test=\"cssClass != null and cssClass != ''\">#{cssClass},</if>",
		"<if test=\"listClass != null and listClass != ''\">#{listClass},</if>",
		"<if test=\"isDefault != null and isDefault != ''\">#{isDefault},</if>",
		"<if test=\"status != null\">#{status},</if>",
		"<if test=\"remark != null and remark != ''\">#{remark},</if>",
		"<if test=\"createBy != null and createBy != ''\">#{createBy},</if>",
		"sysdate()",
		")",
		"</script>"})
	public int insertDictData(DictData dictData);

	/**
	 * 修改字典数据信息
	 * 
	 * @param dictData 字典数据信息
	 * @return 结果
	 */
	@Update({"<script>",
		"update t_dict_data",
		"<set>",
		"<if test=\"dictSort != null\">dict_sort = #{dictSort},</if>",
		"<if test=\"dictLabel != null and dictLabel != ''\">dict_label = #{dictLabel},</if>",
		"<if test=\"dictValue != null and dictValue != ''\">dict_value = #{dictValue},</if>",
		"<if test=\"dictType != null and dictType != ''\">dict_type = #{dictType},</if>",
		"<if test=\"cssClass != null\">css_class = #{cssClass},</if>",
		"<if test=\"listClass != null\">list_class = #{listClass},</if>",
		"<if test=\"isDefault != null and isDefault != ''\">is_default = #{isDefault},</if>",
		"<if test=\"status != null\">status = #{status},</if>",
		"<if test=\"remark != null\">remark = #{remark},</if>",
		"<if test=\"updateBy != null and updateBy != ''\">update_by = #{updateBy},</if>",
		"update_time = sysdate()",
		"</set>",
		"where dict_code = #{dictCode}",
		"</script>"})
	public int updateDictData(DictData dictData);

	/**
	 * 同步修改字典类型
	 * 
	 * @param oldDictType 旧字典类型
	 * @param newDictType 新旧字典类型
	 * @return 结果
	 */
	@Update("update t_dict_data set dict_type = #{newDictType} where dict_type = #{oldDictType}")
	public int updateDictDataType(@Param("oldDictType") String oldDictType, @Param("newDictType") String newDictType);
}
